/*
 * Copyright 2010-2011 WebEqua Team
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package org.mathcloud.math.core.matlab.factory;

//import classes slf4j
import org.mathcloud.math.core.matlab.quantity.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
//import classes org.webequa.core.common
import org.webequa.core.common.factory.QuantityObjectFactory;
import org.webequa.core.common.quantity.*;
import org.webequa.core.common.signature.QuantityObjectSignature;
//import classes org.webequa.core.quantity
//import classes com.mathworks.toolbox.javabuilder
import com.mathworks.toolbox.javabuilder.MWComplexity;
import com.mathworks.toolbox.javabuilder.MWNumericArray;

public class MatlabQuantityObjectFactory implements QuantityObjectFactory
{
	private static final Logger log = LoggerFactory.getLogger(MatlabQuantityObjectFactory.class);
	
	public static MatlabQuantityObject getInstance(QuantityObject in)
	{		
		if (in==null) {
			log.warn("QuantityObject isn't defined");
			return null;
		}	
		if (in instanceof MatlabQuantityObject) {
			log.debug("Convert QuantityObject to MatlabQuantityObject");
			return (MatlabQuantityObject)in;
		}	
		else if (in.getSignature().isRealNumberSignature())
		{
			log.debug("Convert QuantityObject to MatlabRealNumber");
			return new MatlabRealNumber((RealNumber)in);
		}
		else if (in.getSignature().isComplexNumberSignature())
		{			
			log.debug("Convert QuantityObject to MatlabComplexNumber");
			return new MatlabComplexNumber((ComplexNumber)in);
		}
		else if (in.getSignature().isRealMatrixSignature())
		{
			log.debug("Convert QuantityObject to MatlabRealMatrix");
			return new MatlabRealMatrix((RealMatrix)in);
		}
		else if (in.getSignature().isComplexMatrixSignature())
		{
			log.debug("Convert QuantityObject to MatlabComplexMatrix");
			return new MatlabComplexMatrix((ComplexMatrix)in);
		}
		else {
			log.warn("Signature of QuntityObject isn't defined");
			return null;
		}	
	}
	
	public static MatlabQuantityObject getInstance(MWNumericArray mtlbObj)
	{
		if (mtlbObj==null) {
			log.warn("MWNumericArray isn't defined");
			return null;
		}	
		else if (mtlbObj.isEmpty()) {
			log.warn("MWNumericArray is empty");
			return null;
		}
		else 
		{
			MatlabQuantityObject res = null;
			int dims[] = mtlbObj.getDimensions();
			if (dims.equals(new int[]{1,1}))
			{
				if (mtlbObj.complexity()==MWComplexity.REAL)
				{
					log.debug("Convert MWNumericArray to MatlabRealNumber");
					res = new MatlabRealNumber(mtlbObj.getDouble());
					return res;
				}
				else
				{
					log.debug("Convert MWNumericArray to MatlabComplexNumber");
					res = new MatlabComplexNumber(mtlbObj.getDouble(),mtlbObj.getImagDouble());
					return res;					
				}				
			}
			else if(dims.length == 2)
			{
				if (mtlbObj.complexity()==MWComplexity.REAL)
				{
					log.debug("Convert MWNumericArray to MatlabRealMatrix");
					res = new MatlabRealMatrix((double[][])mtlbObj.toDoubleArray());
					return res;
				}
				else
				{
					log.debug("Convert MWNumericArray to MatlabComplexMatrix");
					res = new MatlabComplexMatrix((double[][])mtlbObj.toDoubleArray(),(double[][])mtlbObj.toImagDoubleArray());
					return res;					
				}								
			}
			else {
				log.warn("Dimensions of MWNumericArray isn't supported");
				return null;
			}	
		}
	}
	
	@Override
	public QuantityObject newInstance(QuantityObjectSignature arg0)
	{
		if (arg0.isRealNumberSignature())
			return new MatlabRealNumber();
		else if (arg0.isComplexNumberSignature())
			return new MatlabComplexNumber();
		else if (arg0.isRealMatrixSignature())
			return new MatlabRealMatrix(((MatrixObject)arg0).getNumberOfRows(),((MatrixObject)arg0).getNumberOfCols());
		else if (arg0.isComplexNumberSignature())
			return new MatlabComplexMatrix(((MatrixObject)arg0).getNumberOfRows(),((MatrixObject)arg0).getNumberOfCols());
		else return null;		
	}
		
}
